import 'package:boss/models/commodity.dart';
import 'package:boss/provider/commodity_provider.dart';
import 'package:boss/ui/page/commodity/categories/page/plusCommodityMode/plus_commodity_mode_page.dart';
import 'package:boss/ui/page/commodity/categories/page/searchCommodity/search_commodity_page.dart';
import 'package:boss/ui/widget/image/base_image.dart';
import 'package:hi_common/hi_common.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:scrollable_positioned_list/scrollable_positioned_list.dart';

import 'categoriesCommodityBrowser/categories_commodity_browser.dart';
import 'categories_footer.dart';
import 'categories_search_bar.dart';

class CategoriesBrowser extends StatefulWidget {
  @override
  _CategoriesBrowserState createState() => _CategoriesBrowserState();
}

class _CategoriesBrowserState extends State<CategoriesBrowser> with AutomaticKeepAliveClientMixin {
  ItemScrollController commodityScrollController = ItemScrollController();

  _search(Commodity searchCommodity) {
    var commodityProvider = Provider.of<CommodityProvider>(context, listen: false);

    final searchMenu = commodityProvider.storeMenus.firstWhere((menu) {
      return menu.commodityList.any((commodity) => commodity == searchCommodity);
    }, orElse: () => null);

    if (searchMenu != null) {
      //切换到对应的分类
      final menuIndex = commodityProvider.storeMenus.indexOf(searchMenu);
      commodityProvider.setStoreMenuSelectIndex(menuIndex);

      WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
        //滚动到菜品所在的位置
        final commodityIndex = commodityProvider.currentCommodities.indexWhere((commodity) => commodity == searchCommodity);

        commodityScrollController.jumpTo(index: commodityIndex);
      });
    } else {
      toast('未找到对应的菜品');
    }
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);

    return Stack(
      alignment: Alignment.center,
      children: <Widget>[
        Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            GestureDetector(
              onTap: () async {
                var commodityProvider = Provider.of<CommodityProvider>(context, listen: false);
                var result = await searchCommodity(context, commodityProvider.storeMenus);
                if (result != null) {
                  _search(result);
                }
              },
              behavior: HitTestBehavior.translucent,
              child: CategoriesSearchBar(enabled: false),
            ),
            CategoriesCommodityBrowser(commodityScrollController: commodityScrollController),
            CategoriesFooter(),
          ],
        ),
        Consumer<CommodityProvider>(
          builder: (context, commodityProvider, child) {
            if (commodityProvider.batchType == BatchType.normal) {
              return Positioned(
                bottom: MediaQuery.of(context).padding.bottom,
                child: GestureDetector(
                  onTap: () {
                    Navigator.pushNamed(context, PlusCommodityModePage.pageName);
                  },
                  child: BaseImage('assets/image/plus_commodity_icon.png', width: 134.w, height: 134.w),
                ),
              );
            }

            return SizedBox();
          },
        ),
      ],
    );
  }

  @override
  bool get wantKeepAlive => true;
}
